package com.ev.custom.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ev.custom.domain.DeviceDO;
import com.ev.custom.domain.RepairEventDO;
import com.ev.custom.domain.RepairRecordDO;
import com.ev.custom.service.*;
import com.ev.framework.config.Constant;
import com.ev.framework.utils.ShiroUtils;
import com.ev.system.domain.UserDO;
import com.ev.system.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.ParseException;
import java.util.*;

import com.ev.framework.config.ConstantForDevice;
import com.ev.custom.dao.RepairCheckDao;
import com.ev.custom.domain.RepairCheckDO;


@Slf4j
@Service
public class RepairCheckServiceImpl implements RepairCheckService {
	@Autowired
	private RepairCheckDao repairCheckDao;
	@Autowired
	private RepairEventService repairEventService;
	@Autowired
	private RepairRecordService recordService;
	@Autowired
	private NoticeService noticeService;
	@Autowired
	private UserService userService;
	@Autowired
	private DeviceService deviceService;

	@Override
	public RepairCheckDO get(Long id){
		return repairCheckDao.get(id);
	}
	
	@Override
	public List<RepairCheckDO> list(Map<String, Object> map){
		return repairCheckDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return repairCheckDao.count(map);
	}
	
	@Override
	public int save(RepairCheckDO repairCheck){
		return repairCheckDao.save(repairCheck);
	}
	
	@Override
	public int update(RepairCheckDO repairCheck){
		return repairCheckDao.update(repairCheck);
	}
	
	@Override
	public int remove(Long id){
		return repairCheckDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return repairCheckDao.batchRemove(ids);
	}

	@Override
	public void completeCheck(RepairEventDO event,RepairCheckDO check) {
		int count = 0;
		Long eventId = event.getId();
		RepairRecordDO recordDO = null;
		if (check.getId()==null) {
			// 取出有关该次维修事件的列表

			List<RepairRecordDO> recordDOs = recordService.getByEventId(eventId);
			if (recordDOs.size()>0) {
				// 取出最后一条维修记录
				recordDO = recordDOs.get(0);
				Long recordId = recordDO.getId();
				check.setRecordId(recordId);
				check.setEventId(eventId);
				count = this.repairCheckDao.save(check);
			}
		}
		if (check.getId()!=null) {
			count = update(check);
			recordDO = recordService.get(check.getRecordId());
		}
		if (check.getResult().equals(Constant.TS)) {
			return;
		}
		if (count>0) {
			// 若验收结果为不通过
			if (Objects.equals(check.getResult(), ConstantForDevice.RESULT_UNPASS)) {
				// 设置为维修事件为待处理 Constant.WAITING_DEAL 
				event.setStatus(ConstantForDevice.WAITING_DEAL);
				this.repairEventService.update(event);
				// 设置维修记录为不通过 Constant.RESULT_UNPASS
				recordDO.setStatus(ConstantForDevice.RESULT_UNPASS);
				this.recordService.update(recordDO);
			}
			// 若验收结果为通过
			if (Objects.equals(check.getResult(), ConstantForDevice.RESULT_PASS)) {
				// 设置为已验收  Constant.WAITING_CHECK 
				event.setStatus(ConstantForDevice.ALREADY_CHECK);
				event.setAuditorNow(event.getAuditorOne());
				// 发送消息
				this.repairEventService.update(event);
				// 设置维修记录为通过 Constant.RESULT_PASS
				recordDO.setStatus(ConstantForDevice.RESULT_PASS);
				this.recordService.update(recordDO);
				// 发送消息  向维修事件第一审核人发送消息
				this.sendAudiMsg(event);
			}
		}
	}

	private void sendAudiMsg(RepairEventDO event){
		UserDO userDO = userService.get(event.getCreateBy());
		DeviceDO deviceDO = deviceService.get(event.getDeviceId());
		// 发送消息
		try{
			JSONObject contentDetail = new JSONObject();
			contentDetail.put("id",event.getId());
			//http://36.137.218.96:39/#/repair/index   跳转至 维修管理--> 待审核 页面 进行操作！
			contentDetail.put("url","/repair/index");
			contentDetail.put("activeName", "secondTwo");
			List<Long> toUsers = new ArrayList<>();
			toUsers.add(event.getAuditorOne());
			String content = "您好，"+userDO.getName()+"发现现场"+deviceDO.getName()+"的故障已验收，请及时审核！";
			noticeService.saveAndSendSocket("@需要我审核的维修单", content, event.getId(), contentDetail.toString(),55L, ShiroUtils.getUserId(),toUsers);
			log.info("-------发送维修审核--->第一位审核人成功！");
		}catch (Exception e){
			log.error("------发送维修单审核信息发送失败"+Arrays.toString(e.getStackTrace()));
		}
	}


	@Override
	public RepairCheckDO getByEventId(Long id) {
		return this.repairCheckDao.getByEventId(id);
	}

	@Override
	public List<Map<String,Object>> listForMap(Map<String, Object> param) {
		return this.repairCheckDao.listForMap(param);
	}

}
